package eclipse.lib.scripting;
import java.io.*;

import eclipse.lib.player.Player;
import eclipse.server.*;
import eclipse.util.*;
import eclipse.util.io.*;
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
/**
 * Code that the Server can execute through Scripts
 */
public final class SadScript 
{
	/**
	 * Opens an inputStream using an ERL address
	 * @param eString String to parse
	 * @return InputStream object
	 */
	public final InputStream getERL(String eString)
	{
		return new ERL(eString).openStream();
	}
	/**
	 * Reads a property file - takes input as a File, Section, and a Key
	 * @param file File to open
	 * @param section Section to look up
	 * @param key Key to look up
	 * @return Value that is returned
	 */
	public final String getVar(String file, String section, String key)
	{
		//Create a blank variable to store the value to
		final String var;
		//build the property file Object and load it
		final PropertyFile pFile = new PropertyFile(new ERL(file).openStream());
		//Read the variable
		var = pFile.readProperty(section, key);
		//close the property file.
		pFile.closeProperty();
		//return the variable.
		return var;
	}
	/**
	 * Stores a variable within a property file.
	 * It is recommended to make a PropertyFile Object instead
	 * @param file File to open
	 * @param section Section to read
	 * @param key Key to look up
	 * @param value Value to set
	 */
	public final void putVar(String file, String section, String key, String value)
	{
		final PropertyFile pFile = new PropertyFile(new ERL(file).openStream());
		pFile.writeProperty(value, section, key);
		pFile.closeProperty();
	}
	/**
	 * Sends a String over a socket
	 * @param index Index of the client
	 * @param msg Byte Array to send
	 */
	public final void sendString(int index, String msg)
	{
		Database.client[index].sendMsg(msg);
	}
	/**
	 * Sends a byte array over a socket.
	 * @param index Index of the client
	 * @param msg Byte Array to send
	 */
	public final void SendBytes(int index, byte[] msg)
	{
		Database.client[index].sendMsg(msg);
	}
	/**
	 * Disconnects a client from the server
	 * @param index Client index
	 */
	public final void disconnect(int index)
	{
		Database.threadCache.process_NetThread[index].disconnect();
	}
	/**
	 * Shuts the server down.
	 * This command is the same as shutdown.
	 */
	public final void exit()
	{
		Loader.shutdown();
	}
	/**
	 * Shuts the server down.
	 * This command is the same as exit.
	 */
	public final void shutdown()
	{
		Loader.shutdown();
	}
	/**
	 * Returns the pointer to the Player Object.
	 * @param index Player's defined index (supplied by the Server)
	 * @return Player Object
	 */
	public final Player getPlayer(int index)
	{
		return Database.player[index];
	}
}
